package utils

import (
	"io/ioutil"
	"os"
	"path/filepath"
)

//@author: [songzhibin97](https://github.com/songzhibin97)
//@function: FileMove
//@description: 文件移动供外部调用
//@param: src string, dst string(src: 源位置,绝对路径or相对路径, dst: 目标位置,绝对路径or相对路径,必须为文件夹)
//@return: err error

func FileMove(src string, dst string) (err error) {
	if dst == "" {
		return nil
	}
	src, err = filepath.Abs(src)
	if err != nil {
		return err
	}
	dst, err = filepath.Abs(dst)
	if err != nil {
		return err
	}
	var revoke = false
	dir := filepath.Dir(dst)
Redirect:
	_, err = os.Stat(dir)
	if err != nil {
		err = os.MkdirAll(dir, 0755)
		if err != nil {
			return err
		}
		if !revoke {
			revoke = true
			goto Redirect
		}
	}
	return os.Rename(src, dst)
}

// 获取文件内容
func GetFileContent(filepath string) (content string,err error) {
	f, err := os.OpenFile(filepath, os.O_RDONLY, 0777)
	defer f.Close()
	if err != nil {
		return "", err
	}
	//var b []byte
	b, err := ioutil.ReadAll(f)
	return string(b), nil
}